#include "pointlight.h"
#include "../core/transform.h"
#include "../core/sampling.h"

PointLight::PointLight(const Transform *l2w, const Transform *w2l, Spectrum is) :
			Light(l2w, w2l) {
	lightPos = (*l2w)(Point3(0,0,0));
	intensity = is;
}

Spectrum PointLight::Sample_L(const Point3 &p, Vector3 *wi, VisibilityTester *vis) const {
	*wi = Normalize(lightPos - p);
	vis->SetSegment(p, lightPos);
	// convert from intensity to radiance
	return intensity / DistanceSqured(p, lightPos);
}

Spectrum PointLight::Sample_L(const Point3 &p, const Point2 &sample, Vector3 *wi, float *pdf, VisibilityTester *vis) const {
	*pdf = 1.f;
	return Sample_L(p, wi, vis);
}

float PointLight::Pdf(const Point3 &p, const Vector3 &wi) const {
	return 0.f;
}

Spectrum PointLight::Sample_L(const Scene *scene, const Point2 &lightsample, const Point2 &dirsample, Ray *ray, float *pdf) const {
	ray->o = lightPos;
	ray->d = UniformSamplingSphere(dirsample);
	*pdf = UniformSamplingSpherePdf();
	return intensity;
}